﻿<cfscript>
/** 
* @hint "acadmicBase - FormClass"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.classListener" 
	output=false 
	depends="senateMathAdvice, markAdvice, creditFactory, sequenceFormClass, sessionAdvice"
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	
	/**
	* @hint "刷新班级成绩统计信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function flushClassMarkProfile ( required MachII.framework.Event event ) output=false {
		
		var classId = event.getArg("Class");
		var creditFactory = getcreditFactory();
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT stu_id FROM i_stu_class_info a WHERE a.cls_id = :classId ";

			queryObj = new Query( datasource=application.dnsMaster);
			queryObj.addParam( name="classId", value=classId, cfsqltype="cf_sql_char" );
			
			var rs_student = queryObj.execute( sql=sql ).getResult();
			
			for ( var a=1; a LTE rs_student.recordCount; a++ ) {
				
				var studentId = rs_student["stu_id"][a];
				
				/* 刷新指定学生的学分统计数据 */
				creditFactory.creditRefresh( studentId ); 
				
			}

		}

	}

	/**
	* @hint "根据班级实际在校人数刷新指定专业计划人数"
	* 
	* @event "MachII Event Object"
	*/
	public void function flushClassPlanSizeBySubject( required MachII.framework.Event event ) output=false {
		
		var sql = "";
		var queryObj = "";
		
		var subjectId = event.isArgDefined("SubID");
		
		if ( len(subjectId) ) {
			
			/* Every SQL operation is TRANSACATION*/
			transaction {
				
				/* 第一步 更新班级在校学生人数 */
				sql = "UPDATE t_class a 
						SET 
							a.cls_scale = ( 
								SELECT 
									COUNT(*) 
								FROM t_student b 
								INNER JOIN t_student_status c ON c.stu_id = b.stu_id 
								WHERE 
									a.cls_id = b.cls_id 
									AND 
									SUBSTR( c.student_prop, 1, 1 ) = '1' 
								) 
						WHERE 
							a.sbj_id = :subjectId ";
				
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="subjectId", value=subjectId, cfsqltype="cf_sql_varchar" );
				
				queryObj.execute( sql=sql );
				
				
				/* 第二步 更新班级计划人数 */
				sql = "UPDATE t_class a 
						SET 
							a.cls_size = ( 
								SELECT 
									COUNT(*) 
								FROM t_student b 
								WHERE 
									a.cls_id = b.cls_id 
								) 
						WHERE 
							a.sbj_id = :subjectId 
							AND 
							a.cls_scale > 10 ";
				
				queryObj = new Query( datasource=application.dnsMaster);
				queryObj.addParam( name="subjectId", value=subjectId, cfsqltype="cf_sql_varchar" );
				
				queryObj.execute( sql=sql );

			}
			
		}
		
	}

	
	/**
	* @hint "根据班级实际在校人数刷新计划人数"
	* 
	* @event "MachII Event Object"
	*/
	public void function flushClassPlanSize( required MachII.framework.Event event ) output=false {
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
				
			/* 第一步 更新班级在校学生人数 */
			sql = "UPDATE t_class a 
					SET 
						a.cls_scale = ( 
							SELECT count(*) from i_stu_class_info b 
							WHERE b.cls_id = a.cls_id 
							) ";
			
			queryObj = new Query( datasource=application.dnsMaster);

			queryObj.execute( sql=sql );
				
				
			/* 第二步 更新班级计划人数 */
			sql = "UPDATE t_class a 
					SET 
						a.cls_size = ( 
							SELECT count(*) from i_stu_class_info b 
							WHERE b.cls_id = a.cls_id 
							) 
					WHERE 
						a.cls_scale > 10 ";
				
			queryObj = new Query( datasource=application.dnsMaster);

			queryObj.execute( sql=sql );

		}
		
	}
	
	/**
	* @hint "创建行政班"
	* 
	* @event "MachII Event Object"
	*/
	public void function batchCreateClass( required MachII.framework.Event event ) output=false {
		
		sessionAdvice = getsessionAdvice();
		
		var subject = "";
		var campus = "";
		var class = "";
		var id = "";
		var classFound = "";
		
		var grade = event.getArg("Grade");
		var stuCount = event.getArg("StudentCount");
		var clsName = event.getArg("ClassName");
		var teacherId = event.getArg("TeacherID2");
		var dataStart = replace(event.getArg("DateStart"), "-", "/", "ALL");
		
		var LastMender = sessionAdvice.getAutherUserID();
		
		//var classPrepend = getsenateMathAdvice().divide( stuCount, clsCount);
		var lastModified = dateFormat(now(),"YYYY/M/D H:MM:ss");
		var clsFound = dateFormat(now(),"YYYY/M/D");
		var sequenceFormClass = getsequenceFormClass();
		var i = 0;
		
		var sql = ""; 
		var queryObj = ""; 

		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT  a.sbj_id,
						   a.sbj_short,
						   a.term_start,
						   a.institute_id 
					FROM t_subject a 
					WHERE a.sbj_id = :SubjectID ";
					
			queryObj = new Query( datasource=application.dnsMaster) ;
			
			queryObj.addParam( name="SubjectID", value=event.getArg("SubjectID") , cfsqltype="cf_sql_varchar" );
			
			var rs_subject= queryObj.execute( sql=sql ).getResult();
			
			/* 读取最大的班级编号 */
			sequenceFormClass.setSubjectID(rs_subject.sbj_id);
			sequenceFormClass.setGrade(grade);
			id = sequenceFormClass.getID();
			
			/* 动态生成班级名称 */
			//classname = rs_subject.sbj_short & right(grade, 2) & right(id, 2);
			
			/* 秋季招生 则建班为9月 春季招生为3月 */
			/*if (rs_subject.sbj_id == "1") {
				classFound = grade & "09";
			} 
			else {
				classFound = grade & "03";
			}*/
						
			sql = "INSERT INTO t_class (
						   sbj_id,
						   campus_id,
						   cls_name,
						   cls_found,
						   cls_size,
						   cls_scale,
						   grade,
						   teacher_in_charge,
						   cls_id, 
						   last_mender, 
						   last_modified,
						   cls_start  
					)VALUES (
							:Subject ,
							:Campus ,
							:Name ,
							:classFound ,
							:PlanSize ,
							:RealSize ,
							:Grade ,
							:teacherId,
							:ClassID, 
							:LastMender, 
							:LastModified, 
							:clsStart   
					)";
					
			queryObj = new Query( datasource=application.dnsMaster) ;
			
			queryObj.addParam( name="Subject", value=rs_subject.sbj_id, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="Campus", value=event.getArg("CampusID"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="Name", value=clsName, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="classFound", value=clsFound, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="PlanSize", value=stuCount, cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="RealSize", value=0, cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="Grade", value=grade, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="teacherId", value=teacherId , cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="ClassID", value=id , cfsqltype="cf_sql_char" );
			queryObj.addParam( name="LastMender", value=LastMender , cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="LastModified", value=lastModified , cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="clsStart", value=dataStart , cfsqltype="cf_sql_varchar" );
			
			queryObj.execute( sql=sql );
												
			event.setArg("DepID", rs_subject.institute_id);
			event.setArg("SubID", rs_subject.sbj_id);
			event.setArg("TabID", "labFormClass");
		}
		
	}
	
	/**
	* @hint "删除班主任"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteHeadTeacher( required MachII.framework.Event event ) output=false {
		
		var classId = event.getArg("CLSID");
		
		var sql = "";
		var queryObj = "";
		
		transaction{
			
			sql = "UPDATE t_class SET teacher_in_charge = '' WHERE cls_id = :classId ";
			
			queryObj = new Query( datasource=application.dnsMaster );
			
			queryObj.addParam( name="classId", value=classId, cfsqltype="cf_sql_char" );
			
			queryObj.execute( sql=sql );
			
			event.setArg("DepID", event.getArg("DepID"));
			event.setArg("SubID", event.getArg("SubID"));
			event.setArg("TabID", "labFormClass");
		}
		
	}
	
	/**
	* @hint "修改班主任"
	* 
	* @event "MachII Event Object"
	*/
	public void function UpdateHeadTeacher( required MachII.framework.Event event ) output=false {
		
		var classId = event.getArg("classId");
		var teacherId = event.getArg("TeacherID2");
		
		var sql = "";
		var queryObj = "";
		
		transaction{
			
			sql = "UPDATE t_class SET teacher_in_charge = :teacherId WHERE cls_id = :classId ";
			
			queryObj = new Query( datasource=application.dnsMaster );
			
			queryObj.addParam( name="teacherId", value=teacherId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="classId", value=classId, cfsqltype="cf_sql_char" );
			
			queryObj.execute( sql=sql );
			
			event.setArg("DepID", event.getArg("DepID"));
			event.setArg("SubID", event.getArg("SubID"));
			
			event.setArg("TabID", "labFormClass");
		}
		
	}
	
}

</cfscript>
